package com.changgou.consumer.service.impl;

import com.changgou.consumer.dao.SeckillGoodsMapper;
import com.changgou.consumer.dao.SeckillOrderMapper;
import com.changgou.consumer.service.SecKillOrderService;
import com.changgou.seckill.pojo.SeckillOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author LQQ
 * @Date 2020/3/19 22:27
 * @Version 1.0
 */
@Service
public class SecKillOrderServiceImpl implements SecKillOrderService {
    @Autowired
    private SeckillOrderMapper seckillOrderMapper;
    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    /**
     * 基于业务层完成同步mysql的操作
     *
     * @param seckillOrder
     * @return
     */
    @Transactional
    public int createOrder(SeckillOrder seckillOrder) {
        //1.扣减秒杀商品的库存
        int result = seckillGoodsMapper.updateStockCount(seckillOrder.getSeckillId());

        if (result <= 0) {
            return 0;//更新数据库失败
        }

        //2.新增秒杀订单对象
        int i = seckillOrderMapper.insertSelective(seckillOrder);

        if (i <= 0) {
            return 0;//新增秒杀订单失败
        }
        return 1;
    }
}
